﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Windows.Browser;
using System.Windows.Data;

using EditorialAdministration;

namespace AutomationAdmin.ViewModels
{
    public class AppState : INotifyPropertyChanged
    {
        public AutomationAppList AppList
        {
            get { return App.CurrentAutomationAppList; }
        }

        private string _navPath; 
        public string NavPath
        {
            get { return _navPath; }
            set
            {
                _navPath = value;
                OnPropertyChanged("NavPath");
            }
        }

        private string _userName = string.Empty;
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
                OnPropertyChanged("UserName");
            }
        }

        private PageLinkCollection _pageLinks = new PageLinkCollection();
        public PageLinkCollection PageLinks
        {
            get { return _pageLinks; }
        }

        private List<NewAppRequest> _requests = new List<NewAppRequest>();
        public List<NewAppRequest> Requests
        {
            get { return _requests; }
        }

        private List<SafeUser> _users = new List<SafeUser>();
        public List<SafeUser> Users
        {
            get { return _users; }
        }

        private PagedCollectionView _usersView;
        public PagedCollectionView UsersView
        {
            get { return _usersView; }
        }

        private List<AccessAppRequest> _accessAppRequests = new List<AccessAppRequest>();
        public List<AccessAppRequest> AccessAppRequests
        {
            get { return _accessAppRequests; }
        }

        private PagedCollectionView _accessAppRequestsView;
        public PagedCollectionView AccessAppRequestsView
        {
            get { return _accessAppRequestsView; }
        }

        private List<UserPermission> _userPermissions = new List<UserPermission>();
        public List<UserPermission> UserPermissions
        {
            get { return _userPermissions; }
        }

        public void UpdateRequests(NewAppRequest[] reqs)
        {
            _requests.Clear();
            foreach (NewAppRequest req in reqs)
                _requests.Add(req);
            OnPropertyChanged("Requests");
        }

        public void UpdatePageLinks(PageLink[] pageLinks)
        {
            _pageLinks.Clear();
            foreach (PageLink pageLink in pageLinks)
                _pageLinks.Add(pageLink);
            OnPropertyChanged("PageLinks");
        }

        public void UpdateUsers(SafeUser[] users)
        {
            _users.Clear();
            foreach (SafeUser user in users)
                _users.Add(user);
            _usersView = new PagedCollectionView(_users);
            OnPropertyChanged("Users");
        }

        private void UpdateUserAgainstApp(AutomationApp app)
        {
            foreach (SafeUser user in _usersView)
            {
                bool hasPermission = false;

                if (app == null)
                {
                    user.AgainstApp.CanAdd = false;
                    user.AgainstApp.CanRemove = false;
                    continue;
                }

                foreach (UserPermission permission in _userPermissions)
                {
                    if (user.SafeId == permission.User.SafeId
                        && app.Id == permission.AutomationApp.Id)
                    {
                        hasPermission = true;
                    }
                }

                if(hasPermission)
                {
                    user.AgainstApp.CanAdd = false;
                    user.AgainstApp.CanRemove = true;
                }
                else
                {
                    user.AgainstApp.CanAdd = true;
                    user.AgainstApp.CanRemove = false;
                }
            }
        }

        public void UpdateUserPermissions(UserPermission[] permissions)
        {
            _userPermissions.Clear();

            foreach (UserPermission p in permissions)
                _userPermissions.Add(p);

            

            App.CurrentAutomationAppList.AutomationAppsView.CurrentChanged += delegate(object sender, EventArgs args)
            {
                _accessAppRequestsView.Refresh();
                
                ICollectionView cv = sender as ICollectionView;
                
                if (cv != null)
                {
                    UpdateUserAgainstApp(cv.CurrentItem as AutomationApp);
                    _usersView.Refresh();
                }
            };

            UpdateUserAgainstApp(App.CurrentAutomationAppList.AutomationAppsView.CurrentItem as AutomationApp);

            OnPropertyChanged("UserPermissions");
            OnPropertyChanged("UserPermissionsView");
        }

        public void UpdateAccessAppRequests(AccessAppRequest[] requests)
        {
            _accessAppRequests.Clear();

            foreach (AccessAppRequest r in requests)
                _accessAppRequests.Add(r);

            _accessAppRequestsView = new PagedCollectionView(_accessAppRequests);

            _accessAppRequestsView.Filter = new Predicate<object>(delegate(object item)
            {
                AccessAppRequest r = item as AccessAppRequest;
                if (r != null)
                {
                    return (r.App ==
                        App.CurrentAutomationAppList.AutomationAppsView.CurrentItem);
                }
                return false;
            });

            App.CurrentAutomationAppList.AutomationAppsView.CurrentChanged += delegate(object sender, EventArgs args)
            {
                _accessAppRequestsView.Refresh();
            };

            OnPropertyChanged("AccessAppRequests");
            OnPropertyChanged("AccessAppRequestsView");
        }

        public void UpdatePageLinks(string xamlPath)
        {
            OnPropertyChanged("PageLinks");
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        } 

        #endregion
    }
}
